package com.itheima.reggie.controller;

import com.itheima.reggie.common.Page;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Result;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrdersService ordersService ;

    @RequestMapping("/order/submit")
    @ResponseBody
    public Result add(@RequestBody Orders orders , HttpSession httpSession ){

        //获取用户的id
        Long userId = (Long) httpSession.getAttribute("user");

        //设置属性
        orders.setUserId(userId);

        ordersService.add(orders);

        return Result.success(null);
    }

    //获取所有订单信息
    @RequestMapping("/order/page")
    @ResponseBody
    public Result page (Integer page , Integer pageSize , String number){
        Result result = ordersService.page( page , pageSize , number  );
        return result ;
    }

    @RequestMapping("/order/userPage")
    @ResponseBody
    public Result userPage (Integer page , Integer pageSize , HttpSession httpSession ){
        //获取用户的id
        Long user = (Long) httpSession.getAttribute("user");
        //调用业务层
        Page page1  =  ordersService.findByUserId(page , pageSize , user );
        //返回值
        return Result.success(page1);
    }


}
